<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\ProductsModel;
use App\Models\TypeModel;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
       $product=ProductsModel::paginate(2);
       // dd($product);
      return view("Admin/Products/index")->with("product",$product);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
          $typeM=new TypeModel();
          $types=$typeM->getalldata();

         return view("Admin/Products/create")->with("types",$types);


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $productname=$request->input("productname");
        $typename=$request->input("typename");
        $price=$request->input("price");
        $comp=$request->input("comp");
        $intro=$request->input("intro");
        $products=new ProductsModel();
        $data["productname"]=$productname;
        $data["typename"]=$typename;
        $data["price"]=$price;
        $data["comp"]=$comp;
        $data["intro"]=$intro;
        $products->add($data);
        return redirect("/admin/product");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $one=DB::table('products')->select('products.*')->first();
      
        $typeM=new TypeModel();
        $type=$typeM->get();
       
     
        return view('Admin/Products/edit', compact('one','type'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $productname=$request->input("productname");
        $typename=$request->input("typename");
        $price=$request->input("price");
        $comp=$request->input("comp");
        $intro=$request->input("intro");
        
        $data["productname"]=$productname;
        $data["typename"]=$typename;
        $data["price"]=$price;
        $data["comp"]=$comp;
        $data["intro"]=$intro;
       
        $products=new ProductsModel();
        $products->update1($id,$data);
        return redirect("/admin/product");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
         $product=ProductsModel::where("prductid",$id)->delete();
        return redirect("/admin/product");
    }
}
